//
//  InfoCollectResultViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/27.
//  Copyright © 2020 王洋. All rights reserved.
//


#import <ReactiveObjC.h>

#import "TT_SegmentView.h"
#import "TT_BaseNaviView.h"

#import "TT_BaseTableView.h"

#import "XLPageViewController.h"

#import "infoCollectStoreViewController.h"
#import "infoCollectPackageViewController.h"
#import "infoCollectCaseViewController.h"


#import "InfoCollectResultViewController.h"

@interface InfoCollectResultViewController ()
<UITableViewDelegate,
UITableViewDataSource,
XLPageViewControllerDelegate,
XLPageViewControllerDataSrouce>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) TT_BaseTableView *tableView;
@property (nonatomic, strong) UITableViewCell *cell;
//@property (nonatomic, strong) TT_SegmentView *segView;
@property (nonatomic, strong) NSMutableArray *segArray;
@property (nonatomic ,assign) BOOL canScroll;

@property (nonatomic, strong) TT_BaseNaviView *naviView;
@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong) infoCollectStoreViewController *storeVC;
@property (nonatomic, strong) infoCollectPackageViewController *packageVC;
@property (nonatomic, strong) infoCollectCaseViewController *caseVC;

@end

@implementation InfoCollectResultViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

-(NSMutableArray *)segArray{

    if (!_segArray) {
        _segArray = [NSMutableArray array];
    }
    return _segArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

-(void)initView{
    
    __weak typeof(self)weakSelf = self;
    [[[NSNotificationCenter defaultCenter]rac_addObserverForName:@"tabNoti" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        weakSelf.canScroll = YES;
        [weakSelf subTabViewCanScroll:NO];
    }];
    self.canScroll = YES;
    self.naviView.titleLab.alpha = 0.0f;

    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif

    if (@available(iOS 11.0,*)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];

    UIImageView *headerImg = [[UIImageView alloc] init];
    [headerImg setImage:[UIImage imageNamed:@"infoCollect_banner"]];
    headerImg.frame = CGRectMake(0, 0, TTScreenWidth, TTScreenWidth * 0.52);
    self.tableView.tableHeaderView = headerImg;
    headerImg.autoresizingMask = UIViewAutoresizingNone;

    [self.view addSubview:self.naviView];
    
    [self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(kStatusBarHeight + NavBar_Height);
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return TTScreenHeight;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cell;
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
    
    switch (index) {
        case 0:{
            return self.storeVC;
        } break;
        case 1:{
            return self.packageVC;
        } break;
        case 2:{
            return self.caseVC;
        } break;
    default:
        break;
    }
    
    return self.storeVC;
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return self.titles[index];
}

- (NSInteger)pageViewControllerNumberOfPage {
    return self.titles.count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    NSLog(@"切换到了：%@",self.titles[index]);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.tag !=10086) {
        return;
    }
    
    CGFloat naviH = TTScreenWidth * 0.52 - kStatusBarHeight - NavBar_Height;

    if (scrollView.contentOffset.y>naviH) {
        scrollView.contentOffset = CGPointMake(0, naviH);
        if (self.canScroll) {
            self.canScroll = NO;
            [self subTabViewCanScroll:YES];
        }
    }else{
        if (!self.canScroll) {
            scrollView.contentOffset = CGPointMake(0, naviH);
        }
    }
//    导航栏设置
    CGFloat alpha = (scrollView.contentOffset.y)/(naviH - kStatusBarHeight - NavBar_Height);
    NSLog(@"%.2f/%.2f = %.2f",(scrollView.contentOffset.y),(naviH - kStatusBarHeight - NavBar_Height),alpha);
    [self.naviView.naviBgImageView setImage:[Utilities imageWithColor:[[UIColor whiteColor] colorWithAlphaComponent:alpha]]];
    self.naviView.titleLab.alpha = alpha;
}

-(void)subTabViewCanScroll:(BOOL)canScroll{
    
    for (UIViewController *subVC in self.segArray) {
        
        if ([subVC isKindOfClass:[infoCollectStoreViewController class]]) {
            
            self.storeVC.canScroll = canScroll;
            if (!canScroll) {
                self.storeVC.tableview.contentOffset = CGPointZero;
            }
        }
        
        if ([subVC isKindOfClass:[infoCollectPackageViewController class]]) {
            
            self.packageVC.canScroll = canScroll;
            if (!canScroll) {
                self.packageVC.collectionView.contentOffset = CGPointZero;
            }
        }
        
        if ([subVC isKindOfClass:[infoCollectCaseViewController class]]) {
            
            self.caseVC.canScroll = canScroll;
            if (!canScroll) {
                self.caseVC.collectionView.contentOffset = CGPointZero;
            }
        }
    }
}

- (XLPageViewController *)pageViewController {
    
    if (!_pageViewController) {
        XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
        config.shadowLineColor = UIColorHex(0xff4242);
        config.titleSelectedFont = [UIFont boldSystemFontOfSize:14];
        config.titleNormalFont = [UIFont systemFontOfSize:14];
        config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
        config.separatorLineHidden = true;
        config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
        config.shadowLineWidth = 20;
        config.titleSpace = 0;
        config.titleWidth = TTScreenWidth/3.5;
        _pageViewController = [[XLPageViewController alloc] initWithConfig:config];

        _pageViewController.delegate = self;
        _pageViewController.dataSource = self;
        
        for (NSString *typeTitle in self.titles) {
        
            if ([typeTitle isEqualToString:@"商家"]) {
                [self.segArray addObject:self.storeVC];
            }
    
            if ([typeTitle isEqualToString:@"套餐"]) {
                [self.segArray addObject:self.packageVC];
            }
    
            if ([typeTitle isEqualToString:@"案例"]) {
                [self.segArray addObject:self.caseVC];
            }
        }
        
        [self addChildViewController:self.pageViewController];

    }
    return _pageViewController;
}

-(UITableViewCell *)cell{
    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cid"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cid"];
    }
    [cell addSubview:self.pageViewController.view];
    
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    
    return cell;
}

-(TT_BaseTableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[TT_BaseTableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tag = 10086;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedRowHeight = 0;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (TT_BaseNaviView *)naviView {
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:@"TT_BaseNaviView"owner:nil options:nil].firstObject;
        _naviView.titleLab.text = @"婚礼统筹师";
        [_naviView.backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _naviView;
}

- (NSArray *)titles {
    return @[@"商家",@"套餐",@"案例"];
}

- (infoCollectStoreViewController *)storeVC {
    if (!_storeVC) {
        _storeVC = [[infoCollectStoreViewController alloc] init];
    }
    return _storeVC;
}

- (infoCollectPackageViewController *)packageVC {
    if (!_packageVC) {
        _packageVC = [[infoCollectPackageViewController alloc] init];
    }
    return _packageVC;
}

- (infoCollectCaseViewController *)caseVC {
    if (!_caseVC) {
        _caseVC = [[infoCollectCaseViewController alloc] init];
    }
    return _caseVC;
}

@end
